package com.example.maze;

import android.app.Activity;


import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class StartUpActivity extends Activity implements OnClickListener {

	//the keys for transferring the data picked by the player
	public static final String STORE_PERCENT_KEY = "storePercent";
	public static final String STORE_SIZE_KEY = "storeSize";
	public static final String MODE_ORDINAL_KEY = "gameMode";
	//to set the min value of the seekbars
	private static final int SIZE_DELTA = 4;
	private static final int PERCENT_DELTA = 10;
	//to pick the difficulty
	private SeekBar sizePicker;
	private SeekBar percentPicker;
	//to store the size or percent the player has picked with default values
	private int storeSize = 4;
	private int storePercent = 10;
	
	private Button okButton;
	private RadioGroup mode;
	//giving gamemode a default
	private GameMode gameMode = GameMode.Moves;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.start_up_activity);
		//setting up the bars and buttons for the player to pick their difficulty  
		
		sizePicker = (SeekBar) findViewById(R.id.size);
		percentPicker = (SeekBar) findViewById(R.id.percent);
		okButton = (Button) findViewById(R.id.okButton);
		okButton.setOnClickListener(this);
		mode = (RadioGroup) findViewById(R.id.mode);
		//making the default of 4 align with 0
		sizePicker.setProgress(storeSize - SIZE_DELTA);
		percentPicker.setProgress(storePercent - PERCENT_DELTA);
		mode.setOnClickListener(this);
		sizePicker.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() {
			//making it my min and max
	        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
	            storeSize = progress+SIZE_DELTA;
	        }
	
	        public void onStartTrackingTouch(SeekBar seekBar) {}
	
	        public void onStopTrackingTouch(SeekBar seekBar) {}
		});
		
		percentPicker.setOnSeekBarChangeListener( new SeekBar.OnSeekBarChangeListener() {
	 
	        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
	            storePercent = progress+PERCENT_DELTA;
	        }
	
	        public void onStartTrackingTouch(SeekBar seekBar) {}
	
	        public void onStopTrackingTouch(SeekBar seekBar) {}
		});
	}
	
	public void onRadioButtonClicked(View view) {
	    // Is the button now checked?
	    boolean checked = ((RadioButton) view).isChecked();
	    
	    // Check which radio button was clicked
	    switch(view.getId()) {
	        case R.id.movesMode:
	            if (checked){
	            	gameMode = GameMode.Moves;
	            }
	            break;
	        case R.id.timedMode:
	            if (checked){
	            	gameMode = GameMode.Timed;
	            }
	            break;
	    }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    // Inflate the menu items for use in the action bar
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
	    return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle presses on the action bar items
	    switch (item.getItemId()) {
	        case R.id.score_board:
	        	Intent j = new Intent (this, LeaderBoardActivity.class);
	        	startActivity(j);
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}

	@Override
	public void onClick(View v) {
		//passing on the choices to the maze activity
		Intent i = new Intent(this, MazeActivity.class);
		i.putExtra(STORE_SIZE_KEY, storeSize);
		i.putExtra(STORE_PERCENT_KEY, storePercent);
		i.putExtra(MODE_ORDINAL_KEY, gameMode.ordinal());
		startActivity(i);
		
	}
	
}